package com.example.demo.tool.netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

public class NettyClientHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        ByteBuf byteBuf = Unpooled.copiedBuffer("hello server", CharsetUtil.UTF_8);

        ctx.writeAndFlush(byteBuf);
    }


    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        System.out.println("收到服务端消息....");

        ByteBuf byteBuf = (ByteBuf) msg;

        System.out.println("消息内容是：" + byteBuf.toString(CharsetUtil.UTF_8));

        System.out.println("服务器地址：" + ctx.channel().remoteAddress());
    }

}


